<?php


namespace TbfProtocol;


class PageProtocol implements ProtocolProvider{
    private static $obj = null;
    static function obj(){
        if (self::$obj===null){
            self::$obj = new static();
        }
        return self::$obj;
    }
    private function __construct(){}

    /**
     * 请求开始的时候的回调(调用使用协议的时候)
     * @return void
     */
    function processRequest(){

    }

    /**
     * 处理返回数据
     * @param Response $resp
     * @return void
     */
    function processResponse(Response $resp){
        switch ($resp->type){
            case Response::TYPE_TEMPLATE:
                TbfView($resp->tplName,$resp->data);
                return;
            case Response::TYPE_CLIENT_ERROR:
            case Response::TYPE_INNER_ERROR:
                \TbfHttp::sendHttpCode($resp->http_code);
                eh($resp->msg);
                return;
            case Response::TYPE_REDIRECT:
                \TbfHttp::redirect($resp->url);
                return;
            case Response::TYPE_DATA:
            default:
                throw new \Exception('can not process response type with page protocol');
        }
    }

} 